package com.msconfig.mvp.view.activity;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.geo.msconfig.R;
import com.msconfig.event.PowerOnDataDialogDismissEvent;
import com.msconfig.mvp.view.adapter.TimeZoneAdapter;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.HashSet;

public class SetupSurveyPowerOnDateDialogActivity extends Activity implements View.OnClickListener {
    private ListView lvTimeZone;
    private ArrayList<String> arrayList;
    private TimeZoneAdapter adapter;
    private TextView title;
    private Button btnDetermine;
    private Button btnGiveUp;
    private HashSet hashSet;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_activity_setup_survey_power_on_data_dialog);
        lvTimeZone=findViewById(R.id.lv_time_zone);
        title=findViewById(R.id.title);
        btnDetermine=findViewById(R.id.btn_determine);
        btnGiveUp=findViewById(R.id.btn_giveup);
        btnDetermine.setOnClickListener(this);
        btnGiveUp.setOnClickListener(this);
        arrayList=new ArrayList<>();
        title.setText(R.string.title_power_on_date);
        arrayList.add(getString(R.string.text_monday));
        arrayList.add(getString(R.string.text_tuesday));
        arrayList.add(getString(R.string.text_wednesday));
        arrayList.add(getString(R.string.text_thursday));
        arrayList.add(getString(R.string.text_friday));
        arrayList.add(getString(R.string.text_saturday));
        arrayList.add(getString(R.string.text_sunday));

        adapter=new TimeZoneAdapter(this,arrayList);
        hashSet=new HashSet();
        lvTimeZone.setAdapter(adapter);
        lvTimeZone.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                RelativeLayout rl=view.findViewById(R.id.parent);
                CheckBox checkBox=(CheckBox) view.findViewById(R.id.checkbox);
                if(hashSet.add(position+"")){
                    checkBox.setChecked(true);
                    rl.setBackgroundColor(ContextCompat.getColor(getApplicationContext(),R.color.green));
                }else{
                    hashSet.remove(position+"");
                    checkBox.setChecked(false);
                    rl.setBackgroundColor(ContextCompat.getColor(getApplicationContext(),R.color.white));
                }

            }
        });
    }

    //选择的是哪几项
    public ArrayList<String> showSelectItems(View v) {
        ArrayList<String> list=new ArrayList<>();
        long[] id = lvTimeZone.getCheckedItemIds();
        if (id.length > 0) {
            // 用户至少选择了一位作家
            for (int i = 0; i < id.length; i++) {
                list.add(arrayList.get((int)id[i]));
            }
        }
        return list;
    }


    @Override
    public void onClick(View view) {
        if(view==btnDetermine){
            ArrayList<String> list=showSelectItems(lvTimeZone);
            EventBus.getDefault().post(new PowerOnDataDialogDismissEvent(list));
            hashSet.clear();
            finish();
        }else if(view==btnGiveUp){
            hashSet.clear();
            finish();
        }
    }
}
